#include "BatBoss.h"
#include "Resource.h"


/**--------------------------------------------------------
*Description : Constructor
*Method name : BatBoss
*Parameters : d3ddv, BackBuffer, SpriteHandler, x, y, ID, Object_State, Direction
*Return value : 
--------------------------------------------------------**/
BatBoss::BatBoss(LPDIRECT3DDEVICE9 d3ddv,  LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State, int Direction): Object(x, y)//, ID, Object_State)
{
	this->setIDObjType(ID);	

	this->blood = 8;

	pos_x = x;
	pos_y = y;

	this->pos_x_old = x;
	this->pos_y_old = y;
	
	veloc_x = -(float)BATBOSS_VX;
	veloc_y = 0;
	accel_y = 0;

	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[10]->getFileName()), 125, 0);
	p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[15]->getFileName()), 125, 0);

	//Trang thai unactive
	p_sprite->setFrame(0,1);
	p_sprite->setIndex(0);

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	time_die = 0;	

	is_collission = false;	//chua va cham voi simon
	is_go_point1 = false;	//chua o vi tri point 1

	distance_x = distance_y = 0;
	_x_point1 = x + BATBOSS_DISTANCE_X;
	_y_point1 = y - BATBOSS_DISTANCE_Y;

	_x_point2 = x - BATBOSS_DISTANCE_X;
	_y_point2 = y - BATBOSS_DISTANCE_Y;

	this->obj_state = Object_State;
	direction = Direction;
	if (obj_state == ES_NORMAL)
	{
		if (direction == 1)			//sang trai
		{			
		} 
		else if (direction == 2)	//sang phai
		{			
		}
	} 
	
}

/**--------------------------------------------------------
*Description : Destructor
*Method name : BatBoss
*Parameters : 
*Return value : 
--------------------------------------------------------**/
BatBoss::~BatBoss()
{
	/*if (_sprite != NULL)
	delete _sprite;*/	
}

 
/**--------------------------------------------------------
*Description : render obj
*Method name : render
*Parameters : cameraX, cameraY
*Return value : void
--------------------------------------------------------**/
void BatBoss::render(int cameraX, int cameraY)
{	
	if (obj_state != ES_REMOVE)
	{
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}	
}


/**--------------------------------------------------------
*Description : Get obj Type
*Method name : getObjectType
*Parameters : 
*Return value : OBJECT_TYPE
--------------------------------------------------------**/
OBJECT_TYPE BatBoss::getObjectType()
{
	return OBJECT_ENEMY;
}


/**--------------------------------------------------------
*Description : update
*Method name : update
*Parameters : obj_list, input, gameTime, simon  
*Return value : void
--------------------------------------------------------**/
void BatBoss::update(list<Object*>* obj_list, DxInput* input, float gameTime, Object* simon)
{
	switch (obj_state)
	{
	case  ES_NORMAL:
		if (simon->getPosX() + 100 > this->pos_x)
		{
			changeState(ES_ACTIVING);
		}
		break;

	case ES_ACTIVING:
		move(obj_list, gameTime, simon);		
		break;

	case ES_PAUSE:
		time_die += gameTime;
		if (time_die > 1000)
		{
			changeState(ES_ACTIVING);
		}
		break;

	case ES_DIED:
		time_die += gameTime;
		if (time_die > 1200)
		{
			changeState(ES_REMOVE);
		}
		break;
	
	
	}

	p_sprite->update(gameTime);

}

 
/**--------------------------------------------------------
*Description : move obj
*Method name : move
*Parameters : obj_list, TPF, simon  
*Return value : void
--------------------------------------------------------**/
void BatBoss::move(list<Object*>* obj_list, float TPF, Object* simon)
{
	Object* obj;
	list<Object*>::iterator i;
	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();
	
	pos_x_old = pos_x;
	pos_y_old = pos_y;

	if (is_collission == false)
	{
		//vector cua simon keo batboss
		distance_x = simon->getPosX() - this->pos_x;
		distance_y = simon->getPosY() - this->pos_y;
	}
	else
	{
		if (is_go_point1 == true)
		{
			//vector cua diemX keo batboss
			distance_x = _x_point1 - this->pos_x;
			distance_y = _y_point1 - this->pos_y;
		}
		else
		{
			//vector cua diemX keo batboss
			distance_x = _x_point2 - this->pos_x;
			distance_y = _y_point2 - this->pos_y;
		}
	}
	

	if (distance_x > 0)
	{
		pos_x += BATBOSS_VX * TPF;	
	}
	else
	{
		pos_x += -BATBOSS_VX * TPF;	
	}
	if (distance_y > 0)
	{
		pos_y += BATBOSS_VY * TPF;
	}
	else
	{
		pos_y += -BATBOSS_VY * TPF;
	} 

	////va cham voi simon
	Box box(this->pos_x_old,
		this->pos_y_old - this->height, 
		width, 
		height, 
		BATBOSS_VX * TPF, 
		BATBOSS_VY * TPF);

	Box block(simon->getPosX(),
		simon->getPosY() - simon->getHeight(),
		simon->getWidth(),
		simon->getHeight()/2
		);

	Box boxphase = Collision::GetSweptBroadphaseBox(box);
	if (Collision::AABBCheck(boxphase, block))
	{
		float normalx = 0;
		float normaly = 0;
		float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
		if (0 < collision_time && collision_time < 1)
		{
			//simon->changeState(MS_DIE);
			//Xu ly tut mau cua simon
			//hoac chet
			//isCollission = true;
		}
		is_collission = true;
	}


	//Va cham voi diem X1
	Box blockX(_x_point1,
		_y_point1,
		1,
		1);
	boxphase = Collision::GetSweptBroadphaseBox(box);
	if (Collision::AABBCheck(boxphase, blockX))
	{
		float normalx = 0;
		float normaly = 0;
		float collision_time = Collision::SweptAABB(box, blockX, normalx, normaly);
		if (0 < collision_time && collision_time < 1)
		{
			//simon->changeState(MS_DIE);
			//Xu ly tut mau cua simon
			//hoac chet
			//isCollission = true;
		}
		is_collission = false;
		is_go_point1 = false;
	}

	//Va cham voi diem X2
	Box blockX2(_x_point2,
		_y_point2,
		1,
		1);
	boxphase = Collision::GetSweptBroadphaseBox(box);
	if (Collision::AABBCheck(boxphase, blockX2))
	{
		float normalx = 0;
		float normaly = 0;
		float collision_time = Collision::SweptAABB(box, blockX2, normalx, normaly);
		if (0 < collision_time && collision_time < 1)
		{
			//simon->changeState(MS_DIE);
			//Xu ly tut mau cua simon
			//hoac chet
			//isCollission = true;
		}
		is_collission = false;
		is_go_point1 = true;
	}



	pos_x_old = pos_x;
	pos_y_old = pos_y;

	if (veloc_x >= 0)
	{
		direction = 2;
	} 
	else
	{
		direction = 1;
	}
}


/**--------------------------------------------------------
*Description : change state
*Method name : changeState
*Parameters : state
*Return value : void
--------------------------------------------------------**/
void BatBoss::changeState(int state)
{
	obj_state = state;
	switch (obj_state)
	{
		
	case ES_ACTIVING:
		veloc_x = BATBOSS_VX;
		accel_y = BATBOSS_GRAVITY;

		p_sprite->setFrame(1,3);
		p_sprite->setIndex(1);
		break;
	
	case ES_PAUSE:
		time_die = 0;
		break;

	case ES_DIED:		
		time_die = 0;
		p_sprite = p_light;
		veloc_x = 0;
		break;
	}
}
